﻿/* Copyright 2009-2011 Hewlett-Packard Development Company, L.P. All rights reserved. */
enyo.kind({
	name: "com.rextoy.Calculator",
	kind: enyo.VFlexBox, 
	components: [
			{kind: "ApplicationEvents", onWindowRotated: "windowRotated"},
			{
				name : "makeClickSound",
				kind : "PalmService",
				service : "palm://com.palm.audio/systemsounds",
				method : "playFeedback"
			},
			{kind: "Spacer"},
			{kind: enyo.HFlexBox,
			 components: [
				{kind:"Spacer"},
				{kind: enyo.VFlexBox,
						components: [
							{kind: enyo.HFlexBox, components:[
								{content: "", name:"error" ,className: "calculator-err" },
								{content: "", name:"M" ,className: "calculator-top" },
								{kind: "Spacer", className: "calculator-top"},
								{content: "0", name:"register", className: "calculator-register" }
							]},
							
							{content: "0", name:"result", className: "calculator-result"},					
							
							{kind: enyo.HFlexBox, className: "calculator-mode", components:[
								{kind: "RadioGroup", onChange: "angelModeSelected", components: [
									{label: "Deg"},
									{label: "Rad"},
									{label: "Grad"}
								]},
								
								{kind: "Spacer", className: "calculator-mode"},
								
								{kind: "RadioGroup", name:"radix", onChange: "radixSelected", components: [
									{label: "Bin"},
									{label: "Oct"},
									{label: "Dec"},
									{label: "Hex"}
								]}
								
							]},
							{kind: enyo.HFlexBox,
									components: [
										{kind: enyo.VFlexBox, Flex:1,
											components: [										
												{kind: "Button", name: "fnLg", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "lg", onclick: "funcClick",
													components: [
														{content:"lg", className: "calculator-func-normal"}
													]
												},
												{kind: "Button", name: "fnLn", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "ln", onclick: "funcClick",
													components: [
														{content:"ln", className: "calculator-func-normal"}
													]
												},
												{kind: "Button", name: "fnEX", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "exp", onclick: "funcClick",
													components: [
														{content:"e", className: "calculator-func-base"},
														{content:"x", className: "calculator-func-exp"},
													]
												},
												{kind: "Button", name: "fnXY", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "9", onclick: "operatorClick",
													components: [
														{content:"x", className: "calculator-func-base"},
														{content:"y", className: "calculator-func-exp"}												
													]											
												},
												
												{kind: "Button", name: "fnFac", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "fac", onclick: "funcClick",
													components: [
														{content:"!", className: "calculator-func-normal"}
													]
												},
												{kind: "Button", name: "fn2N", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "pow2n", onclick: "funcClick",
													components: [
														{content:"2", className: "calculator-func-base"},
														{content:"n", className: "calculator-func-exp"}
													]
												},
												{kind: "Button", name: "fnRnd", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "random", onclick: "funcClick",
													components: [
														{content:"Rnd()", className: "calculator-func-normal"}
													]
												},
												{kind: "Button", name: "fnCbrt", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "random", onclick: "cbrtClick",
													components: [
														{content:"3", className: "calculator-root-left"},
														{content:"√¯", className: "calculator-root-right"}
													]
												}
											]
										},
										{kind: enyo.VFlexBox, Flex:1,
											components: [										
												{kind: "Button", name: "fnSin", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "sin", onclick: "funcClick",
													components: [
														{content:"sin", className: "calculator-func-normal"}														
													]
												},
												{kind: "Button", name: "fnCos", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "cos", onclick: "funcClick",
													components: [
														{content:"cos", className: "calculator-func-normal"}
													]
												},
												{kind: "Button", name: "fnTan", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "tan", onclick: "funcClick",
													components: [
														{content:"tan", className: "calculator-func-normal"}
													]
												},
												{kind: "Button", name: "fnSinh", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "sinh", onclick: "funcClick",
													components: [
														{content:"sinh", className: "calculator-func-normal"}												
													]											
												},
												
												{kind: "Button", name: "fnCosh", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "cosh", onclick: "funcClick",
													components: [
														{content:"cosh", className: "calculator-func-normal"}												
													]
												},
												{kind: "Button", name: "fnTanh", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "tanh", onclick: "funcClick",
													components: [
														{content:"tanh", className: "calculator-func-normal"}
													]
												},
												{kind: "Button", name: "fnE", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "E", onclick: "constClick",
													components: [
														{content:"e", className: "calculator-func-normal"}
													]
												},
												{kind: "Button", name: "fnSqrt", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", onclick: "sqrtClick",
													components: [
														{content:"√¯", className: "calculator-func-normal"}
													]
												}
											]
										},
										{kind: enyo.VFlexBox, Flex:1,
											components: [										
												{kind: "Button", name: "fnASin", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "asin", onclick: "funcClick",
													components: [
														{content:"sin", className: "calculator-func-base"},
														{content:"-1", className: "calculator-func-exp"}
													]
												},
												{kind: "Button", name: "fnACos", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "acos", onclick: "funcClick",
													components: [
														{content:"cos", className: "calculator-func-base"},
														{content:"-1", className: "calculator-func-exp"}
													]
												},
												{kind: "Button", name: "fnATan",  className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "atan", onclick: "funcClick",
													components: [
														{content:"tan", className: "calculator-func-base"},
														{content:"-1", className: "calculator-func-exp"}
													]
												},
												{kind: "Button", name: "fnASinh", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "asinh", onclick: "funcClick",
													components: [
														{content:"sinh", className: "calculator-func-base"},
														{content:"-1", className: "calculator-func-exp"}
													]
												},
												
												{kind: "Button", name: "fnACosh", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "acosh", onclick: "funcClick",
													components: [
														{content:"cosh", className: "calculator-func-base"},
														{content:"-1", className: "calculator-func-exp"}
													]
												},
												{kind: "Button", name: "fnATanh",  className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "atanh", onclick: "funcClick",
													components: [
														{content:"tanh", className: "calculator-func-base"},
														{content:"-1", className: "calculator-func-exp"}
													]
												},
												{kind: "Button", name: "fnPi", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", value: "PI", onclick: "constClick",
													components: [
														{content:"π", className: "calculator-func-normal"}												
													]
												},
												{kind: "Button", name: "fnRec", className:"enyo-button-dark calculator-func-button", layoutKind: "HFlexLayout", onclick: "reciClick",
													components: [
														{content:"1/x", className: "calculator-func-normal"}												
													]
												}
											]
										},
										{kind: enyo.VFlexBox, Flex:1,
											components: [
												{kind: "Button", name: "memClear", caption: "MC", className:"calculator-button enyo-button-affirmative", value:"0", onclick: "memoryClick"},
												{kind: "Button", name: "memPlus", caption: "M+", className:"calculator-button enyo-button-affirmative", value:"1", onclick: "memoryClick"},
												{kind: "Button", name: "memMinus", caption: "M-", className:"calculator-button enyo-button-affirmative", value:"2", onclick: "memoryClick"},
												{kind: "Button", name: "memReturn", caption: "MR", className:"calculator-button enyo-button-affirmative", value:"3", onclick: "memoryClick"},												
												
												{kind: "Button", name: "sign", caption: "+/-", className:"calculator-button enyo-button-blue", onclick: "signClick"},
												{kind: "Button", name: "percent", caption: "%", className:"calculator-button enyo-button-blue", onclick: "percentClick"}
											]
										},
										{kind: enyo.VFlexBox, Flex:1,
											components: [
												{kind: "HFlexBox",
													components:[
														{kind: "Button", name: "n13", caption: "D", className:"calculator-button enyo-notification-button-alternate", value: "D", onclick: "numClick"},
														{kind: "Button", name: "n14", caption: "E", className:"calculator-button enyo-notification-button-alternate", value: "E", onclick: "numClick"},
														{kind: "Button", name: "n15", caption: "F", className:"calculator-button enyo-notification-button-alternate", value: "F", onclick: "numClick"}
													]
												},
												{kind: "HFlexBox",
													components:[
														{kind: "Button", name: "n10", caption: "A", className:"calculator-button enyo-notification-button-alternate", value: "A", onclick: "numClick"},
														{kind: "Button", name: "n11", caption: "B", className:"calculator-button enyo-notification-button-alternate", value: "B", onclick: "numClick"},
														{kind: "Button", name: "n12", caption: "C", className:"calculator-button enyo-notification-button-alternate", value: "C", onclick: "numClick"}
													]
												},
												{kind: "HFlexBox",
													components:[
														{kind: "Button", name: "n7", caption: "7", className:"calculator-button enyo-button-blue", value: "7", onclick: "numClick"},
														{kind: "Button", name: "n8", caption: "8", className:"calculator-button enyo-button-blue", value: "8", onclick: "numClick"},
														{kind: "Button", name: "n9", caption: "9", className:"calculator-button enyo-button-blue", value: "9", onclick: "numClick"}
													]
												},
												{kind: "HFlexBox",
													components:[
														{kind: "Button", name: "n4", caption: "4", className:"calculator-button enyo-button-blue", value: "4", onclick: "numClick"},
														{kind: "Button", name: "n5", caption: "5", className:"calculator-button enyo-button-blue", value: "5", onclick: "numClick"},
														{kind: "Button", name: "n6", caption: "6", className:"calculator-button enyo-button-blue", value: "6", onclick: "numClick"},
													]
												},												
												{kind: "HFlexBox",
													components:[
														{kind: "Button", name: "n1", caption: "1", className:"calculator-button enyo-button-blue", value: "1", onclick: "numClick"},
														{kind: "Button", name: "n2", caption: "2", className:"calculator-button enyo-button-blue", value: "2", onclick: "numClick"},
														{kind: "Button", name: "n3", caption: "3", className:"calculator-button enyo-button-blue", value: "3", onclick: "numClick"}
													]
												},
												{kind: "HFlexBox",
													components:[
														{kind: "Button", name: "n0", caption: "0", className:"calculator-button enyo-button-blue", value: "0", onclick: "numClick"},
														{kind: "Button", name: "decimal", caption: ".", className:"calculator-button enyo-button-blue", onclick: "decimalClick"},
														{kind: "Button", caption: "←", className:"calculator-button enyo-button-negative", onclick: "backClick"}
													]
												},
											]
										},
										{kind: enyo.VFlexBox, Flex:1,
											components: [
												{kind: "Button", caption: "Clear", className:"calculator-button enyo-button-negative", onclick: "clearClick"},
												{kind: "Button", caption: "+", className:"calculator-button enyo-button-light", value: "1", onclick: "operatorClick"},
												{kind: "Button", caption: "-", className:"calculator-button enyo-button-light", value: "2", onclick: "operatorClick"},
												{kind: "Button", caption: "×", className:"calculator-button enyo-button-light", value: "3", onclick: "operatorClick"},
												{kind: "Button", caption: "÷", className:"calculator-button enyo-button-light", value: "4", onclick: "operatorClick"},
												{kind: "Button", caption: "=", className:"calculator-button enyo-button-light", onclick: "equalClick"}
											]
										}
									]
							}
						]
				},	
				{kind:"Spacer"}
			]},
			{kind: "Spacer"}
	],
	
	create: function() {
		this.inherited(arguments);
		
		enyo.setAllowedOrientation('right');
		window.PalmSystem.setWindowOrientation("right");
		
		this.model = new com.rextoy.CalcModel();
		this.model.setView(this);
		
		this.$.radix.setValue(2);//Note:default base 10
		this.radixSelected(this.$.radix);
	},
	
	playFeedback: function() {
		this.$.makeClickSound.call({"name": "down2"});
	},
	
	windowRotated: function(inSender) {
		var o = enyo.getWindowOrientation();
	},
	
	angelModeSelected: function(sender) {
		this.model.setAngelMode(sender.getValue());
	},
	
	updateView: function() {
		var opChar;
		switch(this.model.operator){
			case 1:
				opChar = '+';
				break;
				
			case 2:
				opChar = '-';
				break;
				
			case 3:
				opChar = '×';
				break;
				
			case 4:
				opChar = '÷';
				break;
			
			case 9:
				opChar = '^';
				break;
			
			case 0:
				opChar = ' ';
				break;
		}
		this.$.M.setContent(this.model.hasMemory ? 'M ' + this.model.getMemory() : ' ');
		this.$.error.setContent(((this.model.status==5)?'E':' '));
		if(this.model.status==5){
			this.$.result.setContent('Error');
			return;
		}
		if(this.model.radix == 10) {
			this.$.register.setContent(this.model.getRegister() + '' + opChar);
			this.$.result.setContent(this.model.getResult());
		} else {
			this.$.register.setContent(this.model.getRegister().toString(this.model.radix).toUpperCase() + '' + opChar);
			this.$.result.setContent(this.model.getResult().toString(this.model.radix).toUpperCase());
		}		
	},
	
	numClick: function(sender) {
		this.playFeedback();
		this.model.inputNumber(sender.value);
	},
	
	decimalClick: function() {
		this.playFeedback();
		this.model.inputDecimal();
	},
	
	operatorClick: function(sender) {
		this.playFeedback();
		this.model.inputOperator(Number(sender.value));
	},
	
	equalClick: function() {
		this.playFeedback();
		this.model.inputEqual();
	},
	
	clearClick: function() {
		this.playFeedback();
		this.model.inputClear();
	},
	
	backClick: function() {
		this.playFeedback();
		this.model.inputBack();
	},
	
	signClick: function() {
		this.playFeedback();
		this.model.inputSign();
	},
	
	reciClick: function() {
		this.playFeedback();
		this.model.inputReciprocal();
	},
	
	percentClick: function() {
		this.playFeedback();
		this.model.inputPercent();
	},
	
	sqrtClick: function() {
		this.playFeedback();
		this.model.inputSqrt();
	},
	
	cbrtClick: function() {
		this.playFeedback();
		this.model.inputCbrt();
	},
	
	powerClick: function() {
		this.playFeedback();
		this.model.inputPower();
	},
	
	memoryClick: function(sender) {
		this.playFeedback();
		this.model.inputMemory(Number(sender.value));
	},
	
	funcClick: function(sender) {
		this.playFeedback();
		this.model.inputFunc(sender.value);
	},
	
	constClick: function(sender) {
		this.playFeedback();
		this.model.inputConst(sender.value);
	},
	
	radixSelected: function(sender) {
		this.log(sender.value);
		this.toggleFunctionArea(sender.value==2);
		this.toggleMemoryArea(sender.value==2);
		this.toggleMiscArea(sender.value==2);
		this.toggleNumberArea(sender.value);
		var base = 10;
		switch(sender.value){
			case 0:
				base = 2;
				break;
			
			case 1:
				base = 8;
				break;
			
			case 2:
				base = 10;
				break;
			
			case 3:
				base = 16;
				break;
		}
		this.model.setRadix(base);
	},
	
	toggleFunctionArea: function(enable) {
		for(var p in this.$){
			if(p[0]=='f'&&p[1]=='n'){
				var ctrl = this.$[p];
				ctrl.disabled = (!enable);
				ctrl.applyStyle("color", enable ? null : 'silver');
			}
		}
	},
	
	toggleMemoryArea: function(enable) {
		for(var p in this.$){
			if(p[0]=='m'&&p[1]=='e'&&p[2]=='m'){
				var ctrl = this.$[p];
				ctrl.disabled = (!enable);
				ctrl.applyStyle("color", enable ? null : 'silver');
			}
		}
	},
	
	toggleMiscArea: function(enable) {
		var names = ['sign', 'percent', 'decimal'];
		for(var i = 0; i < names.length; i++){
			var ctrl = this.$[names[i]];
			ctrl.disabled = (!enable);
			ctrl.applyStyle("color", enable ? null : 'silver');			
		}
	},
	
	toggleNumberArea: function(base) {
		var max;
		switch(base) {
			case 0://bin
				max = 2;
				break;
				
			case 1://oct
				max = 8;
				break;
				
			case 2://dec
				max = 10;
				break;
				
			case 3://hex
				max = 16;
				break;
		}
		for(var i = 0; i < max; i++){
			var name = 'n' + i;
			this.$[name].disabled = false;
			this.$[name].applyStyle('color', null);
		}
		for(var i = max; i < 16; i++){
			var name = 'n' + i;
			this.$[name].disabled = true;
			this.$[name].applyStyle('color', 'silver');
		}
	}
});